//客户代表管理

interface ICustomerRepresentativeManagentDetailViewModel {
    /**
     * 编号
     */
    id?: string;
    /**
     * 真实姓名
     */
    name: string;
    /**
     * 职位
     */
    job: string;
    /**
     * 手机号
     */
    phoneNumber: string;
    /**
     * 备注
     */
    remarks: string;
    /**
     * 点击事件
     */
    click: () => void;
}

interface ICustomerRepresentativeManagentViewModel extends IBaseViewModel<ICustomerRepresentativeManagentQueryTerms> {
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<CustomerRepresentativeListItemResponse>;
    /**
     * 职位下拉
     */
    jobDropDown: ValueListItemResponse[];
    /**
     * 添加/编辑详情
     */
    detail: ICustomerRepresentativeManagentDetailViewModel;
    /**
     * 删除客户代表
     */
    deleteCustomerRepresentative: (id: string) => void;
    /**
     * 添加客户代表
     */
    addCustomerRepresentative: () => void;
    /**
     * 编辑客户代表
     */
    editCustomerRepresentative: (id: string) => void;
    /**
     * 客户单位名称
     */
    clientName:string;
    /**
     * 列表高
     */
    heightA:number;
}

interface ICustomerRepresentativeManagentQueryTerms {
    /**
     * 真实姓名
     */
    name: string;
    /**
     * 职位
     */
    job: string;
}

class CustomerRepresentativeManagentController extends ListController<ICustomerRepresentativeManagentViewModel, ICustomerRepresentativeManagentQueryTerms> {
    gridApi: uiGrid.IGridApiOf<CustomerRepresentativeListItemResponse>;
    enterpriseId: string;

    constructor($scope: ICustomerRepresentativeManagentViewModel, private customerRepresentativeService: ICustomerRepresentativeService,
        private valueService: IValueService, private $ngBootbox: BootboxService, private $location: ng.ILocationService) {
        super($scope);
        this.init();
    }

    init(): void {
        super.init();
        this.enterpriseId = JSON.parse(window.localStorage.getItem("client").toString()).clientId;
        this.$scope.clientName = JSON.parse(window.localStorage.getItem("client").toString()).clientName;

        this.$scope.deleteCustomerRepresentative = this.showDeleteCustomerRepresentativeDialog;
        this.$scope.addCustomerRepresentative = this.showAddCustomerRepresentativeDialog;
        this.$scope.editCustomerRepresentative = this.showEditCustomerRepresentativeDialog;

        this.$scope.queryTerms = {
            name: "",
            job: ""
        };

        this.$scope.gridOptions = {
            enableSorting: false,
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            paginationPageSizes: [10, 20, 30],
            columnDefs: [
                { name: "序号", field: "index", enableColumnMenu: false },
                { name: "姓名", field: "realName", enableColumnMenu: false },
                { name: "职务", field: "job", enableColumnMenu: false },
                { name: "联系电话", field: 'phoneNumber', enableColumnMenu: false },
                { name: "创建时间", field: 'createTime', enableColumnMenu: false },
                {
                    name: "操作",
                    cellFilter: 'checkmark',
                    cellTemplate: '<a data-toggle="modal" title="编辑" data-target="#customermyModal" class="fa fa-edit m-l-xs primary text-info" ng-click="grid.appScope.editCustomerRepresentative(row.entity.id)"></a> <a class="primary fa fa-trash-o text-danger" title="删除" ng-click="grid.appScope.deleteCustomerRepresentative(row.entity.id)"></a>',
                    enableColumnMenu: false
                }
            ],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
            }
        };
        this.query();
        this.loadJobDropDown();
    }

    loadData(queryTerms: ICustomerRepresentativeManagentQueryTerms, skip: number, count: number): void {
        this.customerRepresentativeService.getList(this.enterpriseId, queryTerms.name, queryTerms.job, skip, count).then(result => {
            this.$scope.gridOptions.totalItems = result.data.total;
            result.data.data.map((item, index) => item.index = skip++ +1);
            this.$scope.gridOptions.data = result.data.data;

            this.$scope.heightA = (this.$scope.gridOptions.data.length * 39) + 95;

            if (skip == 0) {
                this.$scope.gridOptions.paginationCurrentPage = 1;
            }
            this.$scope.querying = false;
        }, error => {
            this.$scope.querying = false;
        });
    }

    /**
     * 加载职位下拉
     */
    loadJobDropDown(): void {
        this.$scope.jobDropDown = this.valueService.getJobTypeList().data;
    }

    /**
     * 删除客户代表
     */
    showDeleteCustomerRepresentativeDialog = (id: string) => {
        this.$ngBootbox.confirm("确定删除此数据吗？").then(e => {
            this.customerRepresentativeService.delete(id).then(result => {
                this.reload();
            });
        });
    }

    showAddCustomerRepresentativeDialog = () => {
        this.$scope.title = "添加客户代表";
        this.$scope.detail = {
            name: "",
            job: "",
            phoneNumber: "",
            remarks: "",
            click: this.addCustomerRepresentative
        }
    }

    addCustomerRepresentative=()=> {
        if (!$("#customerForm").valid()) { return };
        this.$ngBootbox.confirm("确定保存此数据吗？").then(e => {
            this.customerRepresentativeService.add(
                this.enterpriseId,
                this.$scope.detail.name,
                this.$scope.detail.job,
                this.$scope.detail.phoneNumber,
                this.$scope.detail.remarks
            ).then(result => {
                this.reload();
                $('#customermyModal').modal('hide');
            });
        });
    }

    showEditCustomerRepresentativeDialog = (id: string) => {
        this.$scope.title = "编辑客户代表";
        var validator = $('#customerForm').validate();
        validator.form();
        validator.hideErrors();
        $(".error").removeClass("error");
        this.customerRepresentativeService.getDetail(id).then(result => {
            this.$scope.detail = {
                id: id,
                name: result.data.realName,
                job: result.data.job,
                phoneNumber: result.data.phoneNumber,
                remarks: result.data.remarks,
                click: this.editCustomerRepresentative
            };
        });
    }

    editCustomerRepresentative=()=>{
        this.$ngBootbox.confirm("确定保存此数据吗？").then(e => {
            this.customerRepresentativeService.edit(
                this.$scope.detail.id,
                this.enterpriseId,
                this.$scope.detail.name,
                this.$scope.detail.job,
                this.$scope.detail.phoneNumber,
                this.$scope.detail.remarks
            ).then(result => {
                this.reload();
                $('#customermyModal').modal('hide');
            });
        });
    }
}

angular.module("tms").controller("customerRepresentativeManagentController", ["$scope", "customerRepresentativeService", "valueService", "$ngBootbox", "$location", CustomerRepresentativeManagentController]);